﻿#_*_ coding: UTF-8 _*_

from PyQt4 import QtGui, QtCore
from core import constants

class MTextEdit(QtGui.QDialog):
	def __init__(self,parent=None):
		QtGui.QFrame.__init__(self,parent)

		

		self.controlToolBar = QtGui.QToolBar()
		self.controlToolBar.setMaximumHeight(40)

		self.boldAction = QtGui.QAction(QtGui.QIcon(constants.app_path+"\images\bold.gif"),"Bold",self.controlToolBar)
		self.boldAction.setShortcut('Ctrl+B')
		self.connect(self.boldAction,QtCore.SIGNAL("triggered()"),self.boldSelection)

		self.controlToolBar.addAction(self.boldAction)

		self.editor = QtGui.QTextEdit()
		self.editor.setFont(QtGui.QFont("B Nazanin",14))

		self.okButton = QtGui.QPushButton(u"تاييد")
		self.okButton.setMaximumHeight(50)

		self.mlayout = QtGui.QVBoxLayout()
		self.mlayout.addWidget(self.controlToolBar)
		self.mlayout.addWidget(self.editor)
		self.mlayout.addWidget(self.okButton)

		self.setLayout(self.mlayout)
		self.connect(self.okButton,QtCore.SIGNAL("clicked()"),self.okPressed)
		self.resize(QtCore.QSize(800,500))

	def boldSelection(self):
		a = self.editor.currentCharFormat()
		if a.fontWeight() == QtGui.QFont.Black :
			a.setFontWeight(QtGui.QFont.Normal)
		else:	
			a.setFontWeight(QtGui.QFont.Black)	
		self.editor.setCurrentCharFormat(a)

		a = self.editor.textCursor().charFormat()
		if a.fontWeight() == QtGui.QFont.Black :
			a.setFontWeight(QtGui.QFont.Normal)
		else:	
			a.setFontWeight(QtGui.QFont.Black)	
		self.editor.textCursor().setCharFormat(a)

	def okPressed(self):
		self.accept()

	def showMe(self,title,HTML):
		self.setWindowTitle(title)
		self.editor.setHtml(HTML)
		return self.exec_()

	def innerHtml(self):
		return unicode(self.editor.toHtml())

class MTextDialog(QtGui.QMainWindow):
	def __init__(self,parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		self.setMinimumWidth(100)
		self.setMinimumHeight(100)
		self.mainText = QtGui.QTextEdit(self)
		self.mainText.setReadOnly(True)
		self.setLayoutDirection(QtCore.Qt.RightToLeft)
		self.setCentralWidget(self.mainText)
		self.hide()

	def showMe(self,title,message,size=0):
		self.setWindowTitle(title)
		self.mainText.setHtml("<body dir=rtl>"+message+"</body>")
		self.setVisible(True)

	def focusOutEvent(self,event):
		self.setVisible(False)

class MInProgress(QtGui.QDialog):
	def __init__(self, parent=None):
		QtGui.QDialog.__init__(self, parent, QtCore.Qt.FramelessWindowHint)
		
		self.lbMsg = QtGui.QLabel()
		self.lbMsg.setMargin(14)

		loMsg = QtGui.QVBoxLayout()
		loMsg.addWidget(self.lbMsg)

		self.setLayout(loMsg)

	def tellBusy(self, htmlMsg):
		self.lbMsg.setText(u'<font style="font-family: Tahoma; font-size:11pt; font-weight:700;">%s</font>' % htmlMsg)
		self.setModal(True)
		self.show()
		
	def tellDone(self):
		self.hide()

class SharedUi(object):
	
	mainWindow = None

	clip =None
	clipboard = None

	wordPopupMenu = None
	ayePopupMenu = None
	selectionPopupMenu = None
	modulesListMenu = None
	docablesMenu = None

	selectionStartWord = None
	selectionEndWord = None

	textDialog = None
	editDialog = None
	busyDialog = None
	defaultFont = None

	applications = {}

	@staticmethod
	def init():
		SharedUi.wordPopupMenu = QtGui.QMenu()
		SharedUi.ayePopupMenu = QtGui.QMenu()
		SharedUi.selectionPopupMenu = QtGui.QMenu()
		SharedUi.modulesListMenu = QtGui.QMenu(u'مشخصات')	
		SharedUi.docablesMenu = QtGui.QMenu(u'نما')
		SharedUi.textDialog = MTextDialog(SharedUi.mainWindow)
		SharedUi.editDialog = MTextEdit(SharedUi.mainWindow)
		SharedUi.busyDialog = MInProgress(SharedUi.mainWindow)

		SharedUi.defaultFont = QtGui.QFont()
		SharedUi.defaultFont.setFamily("B Lotus")
		SharedUi.defaultFont.setPointSize(14)
		SharedUi.defaultFont.setWeight(50)
		SharedUi.defaultFont.setItalic(False)
		SharedUi.defaultFont.setUnderline(False)
		SharedUi.defaultFont.setStrikeOut(False)
		SharedUi.defaultFont.setBold(False)


class Selection(object):
	Word = 0
	Aye = 1
	Part = 2
	
	@staticmethod
	def selectionType(startWord, endWord):
		SharedUi.selectionStartWord = startWord
		SharedUi.selectionEndWord = endWord

		if startWord==endWord:
			return Selection.Word
		elif (startWord.aye==endWord.aye) and (endWord.n == len(endWord.aye.words)) and (startWord.n==1):
			return Selection.Aye
		else:
			return Selection.Part
		
